package com.mars.basic.client.controller;

import com.mars.basic.allmain.dto.PermissionDto;
import com.mars.basic.allmain.entity.PermissionEntity;
import com.mars.basic.allmain.po.QPermission;
import com.mars.basic.allmain.po.Permission;
import com.mars.basic.client.IPermissionClient;
import com.mars.basic.factory.PermissionFac;
import com.mars.basic.service.PermissionService;
import com.mars.common.utils.AnyWhereConditionPath;
import com.mars.common.utils.Beans;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class PermissionController implements IPermissionClient {
    @Autowired
    private PermissionService service;
    @Override
    public Permission addPermission(@RequestBody PermissionDto dto) {
        PermissionEntity permission = Beans.mapper(dto, PermissionEntity.class);
        permission.setCreateTime(new Date());
        permission.setUpdateTime(new Date());
        service.getPermissionRepository().save(permission);
        PermissionEntity one = service.getPermissionRepository().findOneIfMutil(QPermission.permission.name.eq(permission.getName()));
        return one;
    }

    @Override
    public List<PermissionDto> getList() {
        BooleanExpression where = AnyWhereConditionPath.anyWherePredicate();
        List<PermissionEntity> all = service.getPermissionRepository().findAll(where);
        return all.stream().map(PermissionFac::toDto).collect(Collectors.toList());
    }
}
